<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :span="4">
                    <el-form-item>
                      <div>编 码</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in appearStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>申请人</div>
                      <v-select v-model="query.apply_admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>仓 库</div>
                      <v-select v-model="query.store_id" action="storeList" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>工 单</div>
                      <v-select v-model="query.workorder_id" action="workOrderList" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>负责人</div>
                      <v-select v-model="query.admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item>
                      <div>操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" :disabled="!selection.length" size="mini" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table :data="tableData" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%">
        <el-table-column type="index" label="序号" width="50" align="center" />
        <el-table-column type="selection" width="45" align="center" />
        <el-table-column prop="coding" label="编码" align="center" width="150" />
        <el-table-column prop="name" label="名称" width="150" />
        <el-table-column prop="status" label="状态" width="100" align="center" :formatter="statusFormat" />
        <el-table-column prop="type" label="类别" />
        <el-table-column prop="equipment_id" label="设备" />
        <el-table-column prop="create_date" label="申请时间" />
        <el-table-column prop="priority" label="优先程度" />
        <el-table-column prop="admin" label="负责人" />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import VSelect from '@/components/select'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
import API from '@/api/stock/appear'
import { appearStatus, c } from '@/utils/options'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getStatusFormat'],
  components: {
    VSelect
  },
  mixins: [exportMethod],
  data() {
    return {
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      selection: [],
      tableData: [
        {
          coding: 'ORDER1',
          name: '维修冲压车床',
          status: '待审核',
          type: '纠正性维护',
          equipment_id: '冲压车床',
          create_date: '2020-01-01 12:12:12',
          priority: '第一级优先',
          admin: '王碧强'
        },
        {
          coding: 'ORDER2',
          name: '周末扫除工单',
          status: '代执行',
          type: '预防性保养',
          equipment_id: '冲压车床',
          create_date: '2020-01-01 12:12:12',
          priority: '第一级优先',
          admin: '王碧强'
        }
      ],
      appearStatus,
      appearStatus
    }
  },
  watch: {},
  methods: {
    search() {
      // this.query.page = 1
      // this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    index() {},
    exportHandler() {
      const data = [
        [
          '编码',
          '名称',
          '状态',
          '申请人',
          '仓库',
          '出库时间',
          '工单',
          '验证人员',
          '验证时间',
          '负责人',
          '排序'
        ]
      ]
      let status = ''
      let v_name = ''
      let v_date = ''
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.status === 0) {
            status = '创建'
          } else if (item.status === 1) {
            status = '审核完成'
          } else if (item.status === 2) {
            status = '已领件'
          } else if (item.status === 3) {
            status = '归档'
          }
          if (item.verify_admin) {
            v_name = item.verify_admin.name
            v_date = item.verify_date
          } else {
            v_name = '未审核'
            v_date = '未审核'
          }
          data.push([
            item.coding,
            item.name,
            status,
            item.apply_admin.name,
            item.stores.name,
            item.appear_date,
            item.order.name,
            v_name,
            v_date,
            item.admin.name,
            item.sort
          ])
        })
      }
      const workbook = {
        SheetNames: ['出库单数据'],
        Sheets: {
          '出库单数据': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '出库单数据报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/stock/appear/appear_add')
    },
    updateHandler(id) {
      const url = `/stock/appear/appear_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getStatusFormat(row)
    }
  }
}
</script>
